package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingProjectServiceImpl
 * @Author songyu
 * @Date 2024-07-05
 */
@RequiredArgsConstructor //lombok注解
@Service
public class NursingProjectServiceImpl implements NursingProjectService {


    private final NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {

        //1.设置PageHelper页码和每页显示多少条
        PageHelper.startPage(pageNum,pageSize);

        //2.执行数据库查询列表数据
        Page<NursingProjectVo> page =  nursingProjectMapper.selectByPage(name,status);

        //3.封装PageResponse返回
        PageResponse<NursingProjectVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        //1.先将nursingProjectDTO转换为nursingProject
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);

        //2.调用mapper插入数据
        nursingProjectMapper.insert(nursingProject);

    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        //1.调用mapper根据id查询护理项目 NursingProject
        NursingProject nursingProject = nursingProjectMapper.getById(id);

        //2.将NursingProject转换为NursingProjectVo
        return BeanUtil.toBean(nursingProject,NursingProjectVo.class);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProjectDTO
     */
    @Override
    public void update(NursingProjectDto nursingProjectDTO) {

        //1.将nursingProjectDTO 转换为 nursingProject
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);

        //2.调用mapper修改护理项目
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用或禁用
     * @param id
     * @param status
     */
    @Override
    public void isEnable(Long id, Integer status) {
        nursingProjectMapper.isEnable(id,status);
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void deleteById(Long id) {

         nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> selectAll() {
        return nursingProjectMapper.selectAll();
    }

}